typedef struct Node  
{  
    int data;  
    struct Node *pNext;  
    struct Node *pPre;  
}NODE, *pNODE;  
  
//创建双向循环链表  
pNODE CreateDbCcLinkList(void);  
  
//打印链表  
void TraverseDbCcLinkList(pNODE pHead);  
  
//判断链表是否为空  
int IsEmptyDbCcLinkList(pNODE pHead);  
  
//计算链表的长度  
int GetLengthDbCcLinkList(pNODE pHead);  
  
//向链表中插入节点  
int InsertEleDbCcLinkList(pNODE pHead, int pos, int data);  
  
//从链表中删除节点  
int DeleteEleDbCcLinkList(pNODE pHead, int pos);  
  
//删除整个链表，释放内存  
void FreeMemory(pNODE *ppHead);  
//创建双向循环链表  
pNODE CreateDbCcLinkList(void)  
{  
    int i, length = 0, data = 0;  
    pNODE p_new = NULL, pTail = NULL;  
    pNODE pHead = (pNODE)malloc(sizeof(NODE));  
  
    if (NULL == pHead)  
    {  
        printf("内存分配失败！\n");  
        exit(EXIT_FAILURE);  
    }  
    pHead->data = 0;  
    pHead->pNext = pHead;  
    pHead->pPre = pHead;  
    pTail = pHead;  
  
    printf("请输入想要创建链表的长度：");  
    scanf("%d", &length);  
  
    for (i=1; i<length+1; i++)  
    {  
        p_new = (pNODE)malloc(sizeof(NODE));  
  
        if (NULL == p_new)  
        {  
            printf("内存分配失败！\n");  
            exit(EXIT_FAILURE);  
        }  
  
        printf("请输入第%d个节点元素值：", i);  
        scanf("%d", &data);  
  
        p_new->data = data;  
        p_new->pPre = pTail;  
        p_new->pNext = pHead;  
        pTail->pNext = p_new;  
        pHead->pPre = p_new;  
        pTail = p_new;  
    }  
    return pHead;  
}  
//打印链表  
void TraverseDbCcLinkList(pNODE pHead)  
{  
    pNODE pt = pHead->pNext;  
  
    printf("链表打印如：");  
    while (pt != pHead)  
    {  
        printf("%d ", pt->data);  
        pt = pt->pNext;  
    }  
    putchar('\n');  
}  
  
//判断链表是否为空  
int IsEmptyDbCcLinkList(pNODE pHead)  
{  
    pNODE pt = pHead->pNext;  
  
    if (pt == pHead)  
        return 1;  
    else  
        return 0;  
}  
  
//计算链表的长度  
int GetLengthDbCcLinkList(pNODE pHead)  
{  
    int length = 0;  
    pNODE pt = pHead->pNext;  
  
    while (pt != pHead)  
    {  
        length++;  
        pt = pt->pNext;  
    }  
    return length;  
}  
//向链表中插入节点  
int InsertEleDbCcLinkList(pNODE pHead, int pos, int data)  
{  
    pNODE p_new = NULL, pt = NULL;  
    if (pos > 0 && pos < GetLengthDbCcLinkList(pHead) + 2)  
    {  
        p_new = (pNODE)malloc(sizeof(NODE));  
  
        if (NULL == p_new)  
        {  
            printf("内存分配失败！\n");  
            exit(EXIT_FAILURE);  
        }  
  
        while (1)  
        {  
            pos--;  
            if (0 == pos)  
                break;  
            pHead = pHead->pNext;  
        }  
          
        p_new->data = data;  
        pt = pHead->pNext;  
        p_new->pNext = pt;  
        p_new->pPre = pHead;  
        pHead->pNext = p_new;  
        pt->pPre = p_new;  
          
        return 1;  
    }  
    else  
        return 0;  
}  
  
//从链表中删除节点  
int DeleteEleDbCcLinkList(pNODE pHead, int pos)  
{  
    pNODE pt = NULL;  
    if (pos > 0 && pos < GetLengthDbCcLinkList(pHead) + 1)  
    {  
        while (1)  
        {  
            pos--;  
            if (0 == pos)  
                break;  
            pHead = pHead->pNext;  
        }  
        pt = pHead->pNext->pNext;  
        free(pHead->pNext);  
        pHead->pNext = pt;  
        pt->pPre = pHead;  
  
        return 1;  
    }  
    else  
        return 0;     
}  
//删除整个链表，释放内存空间  
void FreeMemory(pNODE *ppHead)  
{  
    pNODE pt = NULL;  
    while (*ppHead != NULL)  
    {  
        pt = (*ppHead)->pNext->pNext;  
  
  
        if ((*ppHead)->pNext == *ppHead)  
        {  
            free(*ppHead);  
            *ppHead = NULL;  
        }  
        else  
        {  
            free((*ppHead)->pNext);  
            (*ppHead)->pNext = pt;  
            pt->pPre = *ppHead;  
        }  
    }  
}  
int main(void)  
{  
    int flag = 0, length = 0;  
    int position = 0, value = 0;  
    pNODE head = NULL;  
  
    head = CreateDbCcLinkList();  
      
    flag = IsEmptyDbCcLinkList(head);  
    if (flag)  
        printf("双向循环链表为空！\n");  
    else  
    {  
        length = GetLengthDbCcLinkList(head);  
        printf("双向循环链表的长度为：%d\n", length);  
        TraverseDbCcLinkList(head);  
    }  
      
    printf("请输入要插入节点的位置和元素值(两个数用空格隔开)：");  
    scanf("%d %d", &position, &value);  
    flag = InsertEleDbCcLinkList(head, position, value);  
    if (flag)  
    {  
        printf("插入节点成功！\n");  
        TraverseDbCcLinkList(head);  
    }     
    else  
        printf("插入节点失败！\n");  
      
    flag = IsEmptyDbCcLinkList(head);  
    if (flag)  
        printf("双向循环链表为空，不能进行删除操作！\n");  
    else  
    {  
        printf("请输入要删除节点的位置：");  
        scanf("%d", &position);  
        flag = DeleteEleDbCcLinkList(head, position);  
        if (flag)  
        {  
            printf("删除节点成功！\n");  
            TraverseDbCcLinkList(head);  
        }     
        else  
            printf("删除节点失败！\n");  
    }  
          
    FreeMemory(&head);  
    if (NULL == head)  
        printf("已成功删除双向循环链表，释放内存完成！\n");  
    else  
        printf("删除双向循环链表失败，释放内存未完成！\n");  
  
    return 0;  
}  